package cn.hnu.qq.global;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;

import javax.swing.BoundedRangeModel;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;



public class FileReceiverThread extends Thread
{
   
    private java.net.Socket socket = null;
    private java.io.InputStream in = null;//�ӿͻ��˶���ݵ�
    private java.io.PrintWriter out = null;//��ͻ���д���
	private long diff;
    private JProgressBar progress;
	private Integer filesizes;

	private boolean sendOver = false;
	private String filepath = "";
	private String directory = "E://";
	
    public FileReceiverThread (Socket socket)
    {
            try 
            {
                this.socket = socket;   
            this.in = this.socket.getInputStream();
                this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())),true);
            } 
            catch (IOException e) 
            {// TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    public FileReceiverThread (Socket socket,String dir)
    {
    		this.directory = dir;
            try 
            {
                this.socket = socket;   
            this.in = this.socket.getInputStream();
                this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())),true);
            } 
            catch (IOException e) 
            {// TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
	public String getFilepath() {
		return filepath;
	}

	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}

	public void run()
    {
         String info  = "";
        try
        {      
            while(true)   
            {   
                //��һ������Ϊ����   
                Date befor = new Date();
                
     
                byte cmd[] = new byte[128];   
                int b = 0;   
                while(b<cmd.length)
                { 
                    b += in.read(cmd, b, cmd.length-b);   
                }   
                int ends = 0;   
                for(int i=0;i<cmd.length;i++)
                {
                    if(cmd[i]==-0)
                    {   
                        ends = i;   
                        break;   
                    }   
                }   
                String cmds = new String(cmd,0,ends);   
                if("cp".equals(cmds))
                {   
                    byte[] filename = new byte[256];   
                    b = 0;   
                    while(b<cmd.length)
                    { 
                        b += in.read(filename, b, filename.length-b);   
                    }   
                    ends = 0;   
                    for(int i=0;i<cmd.length;i++)
                    { 
                        if(filename[i]==-0)
                        {   
                            ends = i;   
                            break;   
                        }   
                    }   
                    String filenames = new String(filename,0,ends);
                    filenames = directory  +filenames;
                    System.out.println("filenames--->"+filenames);
                    File fileout = new File(filenames);   
                    
                    
                    FileOutputStream fos = new FileOutputStream(fileout);   
                       
                    byte[] filesize = new byte[64];   
                    b = 0;   
                    while(b<filesize.length)
                    { 
                        b += in.read(filesize, b, filesize.length-b);   
                    }   
                       
                    ends = 0;   
                    for(int i=0;i<filesize.length;i++)
                    {
                        if(filesize[i]==-0)
                        {   
                            ends = i;   
                            break;   
                        }   
                    }   
                    filesizes = Integer.valueOf(new String(filesize,0,ends));   
                       
                    System.out.println("filesize:"+filesizes);   
                    int ta = (filesizes);   
                    progress.setMaximum(filesizes);
                    progress.setMinimum(0);
                    byte[] buf = new byte[1024*10];   
  
                    while(true)
                    {   
                        if(ta==0)
                        {   
                            break;   
                        }   
                        int len = ta;   
                        if(len>buf.length)
                        {   
                            len = buf.length;   
                        }   
                        int rlen = in.read(buf, 0, len);   

                        ta -= rlen;   
                        if(rlen>0)
                        {   
                            fos.write(buf,0,rlen);   
                            fos.flush();   
                            progress.setValue(filesizes - ta);
                        }   
                        else
                        {   
                            break;   
                        }   
                    }   
                    Date after = new Date();
                    diff =after.getTime() - befor.getTime();
                    diff /=1000;
                   fos.close(); 
                   sendOver = true;
                   break;   
                }   
            }   
            //socket.close();   
            }
        catch(Exception e)
        {
        	JOptionPane.showMessageDialog(null, "文件接收失败");
            e.printStackTrace();   
        }   
          try 
            {
                this.socket.close();
            } 
            catch (IOException ex1)
            {
            }
    }

	public JProgressBar getProgress() {
		return progress;
	}

	public void setProgress(JProgressBar progress) {
		this.progress = progress;
	}

	public boolean isSendOver() {
		return sendOver;
	}
    
}

